//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        ordersLength: 0,

        importordersLength: 0,
        allordersLength: 0,
        tabArr: {
            curHdIndex: 0,
            curBdIndex: 0
        },
        currentTab: 0,
        tabArray: ["已审订单", "未审订单", "订单汇总"],
        membercashlogs:[],
        payallmsg:"",
        payallcount:"",
        payallerrorcount: "",
    },

    onLoad: function (options) {
        this.getMyCashLog();
    },
    getMyCashLog: function () {
        var $that = this;
        var m_type = $that.data.currentTab == 0 ? 1 : 0;
        console.log("函数执行");
        var memberinfo = wx.getStorageSync("memberinfo");
        app.util.request({
            url: 'entry/wxapp/CommissionLog',
            data: {
                "m": 'nets_haojk',
                "memberid": memberinfo.memberid,
                "page": 1,
            },
            method: 'post',
            success: function (response) {
                console.log(response.data.data);
                $that.setData({ "membercashlogs": response.data.data });
            }
        })
    },
    //事件处理函数
    tabFun: function (e) {
        var $that = this;
        //获取触发事件组件的dataset属性  
        var _datasetId = e.target.dataset.id;
        var _obj = {};
        _obj.curHdIndex = _datasetId;
        _obj.curBdIndex = _datasetId;
        this.setData({
            tabArr: _obj
        });
        console.log(_obj.curHdIndex);
        if (_obj.curHdIndex == 0) {
            //$that.getMyOrder("orders");
        } else if (_obj.curHdIndex == 1) {
            console.log("点击了1")
            //$that.getMyOrder("importorders");
        } else if (_obj.curHdIndex == 2) {
            //$that.getMyOrder("orders");
        }
    },
    cashPay: function (e) {
        console.log(e);
        var id = e.currentTarget.dataset.id;
        var $that = this;
        console.log("开始老去")
        app.util.request({
            url: 'entry/wxapp/CashPay',
            showLoading: true,
            data: {
                "m": 'nets_haojk',
                "id": id
            },

            method: 'post',
            success: function (response) {
                var res = response.data;
                console.log(res);
                if(res.data.errno!=undefined){
                    wx.showModal({
                        title: '错误',
                        content: res.data.message,
                        success: function (res) {
                        }
                    });
                    return;
                }
                
                wx.showModal({
                    title: '提示',
                    content: res.message,
                    success: function (res) {
                    }
                });
            }
        });
    },
    docashPay:function(id){
        var $that=this;
        var payallmsg = $that.data.payallmsg;
        var payallcount = $that.data.payallcount;
        var payallerrorcount = $that.data.payallerrorcount;

            payallcount=parseInt(payallcount)+1;
            $that.setData({"payallcount":payallcount});
        app.util.request({
            url: 'entry/wxapp/CashPay',
            showLoading: true,
            data: {
                "m": 'nets_haojk',
                "id": id
            },
            method: 'post',
            success: function (response) {
                var res = response.data;
                console.log(res);
                if (res.data.errno != undefined) {
                    payallerrorcount = parseInt(payallerrorcount) + 1;
                    $that.setData({ "payallerrorcount": payallerrorcount });
                    return;
                }
                if(res.data){

                }else{
                    payallerrorcount = parseInt(payallerrorcount) + 1;
                    $that.setData({ "payallerrorcount": payallerrorcount });
                }
                
            }
        });
    },
    payall:function(){
        var $that=this;
        wx.showModal({
            title: '提示',
            content: "是否要一键打款？请确认账户余额充足，否则会打款失败！",
            success: function (res) {
                if (res.confirm) {
                    console.log('用户点击确定');
                    var cashlist = $that.data.membercashlogs;
                    console.log(cashlist);
                    for (var i = 0; i < cashlist.length; i++) {
                        if(cashlist[i].type==5 && cashlist[i].status==0){
                            $that.docashPay(cashlist[i].id);
                        }
                    }
                    var payallmsg = $that.data.payallmsg;
                    var payallcount= $that.data.payallcount;
                    var payallerrorcount= $that.data.payallerrorcount;
                    wx.showModal({
                        title: '提示',
                        content: "打款请求已发送，请等待结果",
                        success: function (res) {
                        }
                    });
                } else if (res.cancel) {
                    console.log('用户点击取消');
                    return;
                }
            }
        });
        
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../detail/index'
        })
    },

    //底部列表条转  
    skipToOrder: function () {
        wx.redirectTo({
            url: '../partnerOrder/index',
        })
    },
    skipToIndex: function () {
        wx.redirectTo({
            url: '../partnerIndex/index',
        })
    },
    skipToMember: function () {
        wx.redirectTo({
            url: '../partnerMember/index',
        })
    },
    skipToFinance: function () {
        wx.redirectTo({
            url: '../partnerFinance/index',
        })
    }
})
